package org.jeecg.modules.demo.peihebumenbiao.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.v3.oas.models.security.SecurityScheme;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.peihebumenbiao.entity.JieshouDto;
import org.jeecg.modules.demo.peihebumenbiao.entity.Peihebumenbiao;
import org.jeecg.modules.demo.peihebumenbiao.service.IPeihebumenbiaoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.zhidingzhouqigongzuo.entity.ZhidingListDto;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.entity.Zhidingzhouqigongzuo;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.service.IZhidingzhouqigongzuoService;
import org.jeecg.modules.demo.zhouqibiao.entity.Zhouqibiao;
import org.jeecg.modules.demo.zhouqibiao.service.IZhouqibiaoService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
 /**
 * @Description: 配合部门任务表
 * @Author: jeecg-boot
 * @Date:   2025-10-09
 * @Version: V1.0
 */
@Tag(name="配合部门任务表")
@RestController
@RequestMapping("/peihebumenbiao/peihebumenbiao")
@Slf4j
public class PeihebumenbiaoController extends JeecgController<Peihebumenbiao, IPeihebumenbiaoService> {
	@Autowired
	private IPeihebumenbiaoService peihebumenbiaoService;
	@Autowired
	private ISysUserRoleService sysUserRoleService;
	@Autowired
	private IZhidingzhouqigongzuoService zhidingService;
	@Autowired
	private IZhouqibiaoService zhouqibiaoService;
	/**
	 * 分页列表查询
	 *
	 * @param peihebumenbiao
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "配合部门任务表-分页列表查询")
//	@Operation(summary="配合部门任务表-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<IPage<Peihebumenbiao>> queryPageList(Peihebumenbiao peihebumenbiao,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//
//
//        QueryWrapper<Peihebumenbiao> queryWrapper = QueryGenerator.initQueryWrapper(peihebumenbiao, req.getParameterMap());
//		Page<Peihebumenbiao> page = new Page<Peihebumenbiao>(pageNo, pageSize);
//		IPage<Peihebumenbiao> pageList = peihebumenbiaoService.page(page, queryWrapper);
//		return Result.OK(pageList);
//	}
	 //接收工作页面
	 //要考虑部长、配合主管看见的数据与自己角色保持一致
	@GetMapping(value="/list")
	public Result<IPage<JieshouDto>> list(Peihebumenbiao peihebumenbiao,
										  @RequestParam(name="pageNo",defaultValue = "1") Integer pageNo,
										  @RequestParam(name="pageName",defaultValue = "10") Integer pageSize,
										  HttpServletRequest  req){
		//判断登录用户角色
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userId = loginUser.getId();
		QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id",userId);
		SysUserRole sysUserRole = sysUserRoleService.getOne(queryWrapper);
		String roleId = sysUserRole.getRoleId();
		IPage<JieshouDto> dto = new Page<>();
		List<JieshouDto> listDto = new ArrayList<>();
		//部长
		if(roleId.equals("1970852625298092033")){
			Page<Peihebumenbiao> page = new Page<>();
			QueryWrapper<Peihebumenbiao> queryWrapper1 = new QueryWrapper<>();
			queryWrapper1.eq("peihebumenid",loginUser.getOrgId());
			queryWrapper1.eq("zhuangtai","部长待接收");
			IPage<Peihebumenbiao> pageList = peihebumenbiaoService.page(page, queryWrapper1);
			List<Peihebumenbiao> list = pageList.getRecords();
			for(int i = 0;i<list.size();i++){
				JieshouDto jieshouDto = new JieshouDto();
				BeanUtils.copyProperties(list.get(i),jieshouDto);
				jieshouDto.setGongzuoneirong(list.get(i).getXiezuoneirong());
				QueryWrapper<Zhidingzhouqigongzuo> queryWrapper2 = new QueryWrapper<>();
				queryWrapper2.eq("id",list.get(i).getZhidingbiaoid());
				Zhidingzhouqigongzuo zhidingzhouqigongzuo = zhidingService.getOne(queryWrapper2);
				jieshouDto.setWanchengshijian(zhidingzhouqigongzuo.getXiacihuibaoshijian());
				jieshouDto.setGuikoubumen(zhidingzhouqigongzuo.getZhuguanbumenid());
				listDto.add(jieshouDto);
			}
		}
		//主管
		else if (roleId.equals("1976183850854342657")) {
			//获取主管需要接收的数据
			Page<Peihebumenbiao> page = new Page<>();
			QueryWrapper<Peihebumenbiao> queryWrapper1 = new QueryWrapper<>();
			queryWrapper1.eq("zhuangtai","主管待接收");
			queryWrapper1.eq("peihezhuguanid",loginUser.getId());
			IPage<Peihebumenbiao> pageList = peihebumenbiaoService.page(page, queryWrapper1);
			List<Peihebumenbiao> list = pageList.getRecords();
			for(int i = 0;i<list.size();i++){
				JieshouDto jieshouDto = new JieshouDto();
				BeanUtils.copyProperties(list.get(i),jieshouDto);
				jieshouDto.setGongzuoneirong(list.get(i).getXiezuoneirong());
				QueryWrapper<Zhidingzhouqigongzuo> queryWrapper2 = new QueryWrapper<>();
				queryWrapper2.eq("id",list.get(i).getZhidingbiaoid());
				Zhidingzhouqigongzuo zhidingzhouqigongzuo = zhidingService.getOne(queryWrapper2);
				jieshouDto.setWanchengshijian(zhidingzhouqigongzuo.getXiacihuibaoshijian());
				jieshouDto.setGuikoubumen(zhidingzhouqigongzuo.getZhuguanbumenid());
				jieshouDto.setZhuguanpeihe("配合");
				listDto.add(jieshouDto);
			}
			//获取主管负责的数据
			QueryWrapper<Zhouqibiao> queryWrapper3 = new QueryWrapper<>();
			queryWrapper3.eq("wanchengqingkuang","进行中");
			List<Zhouqibiao> list2 = zhouqibiaoService.list(queryWrapper3);
			for(int i = 0;i<list2.size();i++){
				QueryWrapper<Zhidingzhouqigongzuo> queryWrapper4 = new QueryWrapper<>();
				queryWrapper4.eq("id",list2.get(i).getZhidingbiaoid());
				queryWrapper4.eq("zerenzhuguanid",userId);
				List<Zhidingzhouqigongzuo> list3 = zhidingService.list(queryWrapper4);
				if(list3.size()>0){
					for(int j=0;j<list3.size();j++){
						JieshouDto jieshouDto = new JieshouDto();
						jieshouDto.setId(list2.get(i).getId());
						jieshouDto.setZhidingbiaoid(list2.get(i).getZhidingbiaoid());
						jieshouDto.setWanchengshijian(list2.get(i).getWanchengshijian());
						jieshouDto.setGongzuoneirong(list3.get(j).getGongzuoneirong());
						jieshouDto.setZhiliangyaoqiu(list3.get(j).getZhiliangyaoqiu());
						jieshouDto.setGuikoubumen(list3.get(j).getZhuguanbumenid());
						jieshouDto.setZhuguanpeihe("主管");
						listDto.add(jieshouDto);
					}
				}
			}
		}
		//其他
		else{
			return Result.error("没有权限");
		}
		dto.setRecords(listDto);
		return Result.OK(dto);
	}
	/**
	 *   添加
	 *
	 * @param zhibiaoListDto
	 * @return
	 */
	@AutoLog(value = "配合部门任务表-添加")
	@Operation(summary="配合部门任务表-添加")
	@RequiresPermissions("peihebumenbiao:y_peihebumenbiao:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ZhidingListDto zhibiaoListDto) {
		Peihebumenbiao peihebumenbiao = new Peihebumenbiao();
//		BeanUtils.copyProperties(zhibiaoListDto.getYPeihebumenbiaoList(),peihebumenbiao);
		peihebumenbiaoService.save(peihebumenbiao);
		return Result.OK("添加成功！");
	}

	//接受驳回接口
	 //修改，根据不同的用户操作更新不同的状态
	@PostMapping(value="/jieshoubohui")
	public Result<String> jieshoubohui(@RequestBody JieshouDto jieshouDto){
		Peihebumenbiao peihebumenbiao = peihebumenbiaoService.getById(jieshouDto.getId());
		if(jieshouDto.getZhuangtai().equals("已接收")){
			LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			String userId = loginUser.getId();
			QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("user_id",userId);
			SysUserRole sysUserRole = sysUserRoleService.getOne(queryWrapper);
			String roleId = sysUserRole.getRoleId();
			//部长接收
			if(roleId.equals("1970852625298092033")){
				peihebumenbiao.setZhuangtai("主管待接收");
				peihebumenbiaoService.updateById(peihebumenbiao);
			}
			//主管接收
			else if (roleId.equals("1976183850854342657")) {
				peihebumenbiao.setZhuangtai("已接受");
				peihebumenbiaoService.updateById(peihebumenbiao);
			}
		}else if(jieshouDto.getZhuangtai().equals("已驳回")){
			peihebumenbiao.setZhuangtai("已驳回");
			peihebumenbiaoService.updateById(peihebumenbiao);
		}else{
			return Result.error("没有权限");
		}
		return Result.OK("操作成功！");
	}

	//配合主管列表
	 @GetMapping(value = "/peihezhuguan")
	 public Result<List<SysUser>> peihezhuguan(){
		List<SysUser> list =peihebumenbiaoService.getUserList();
		return Result.OK(list);
	 }

	 //选择配合主管
	 @PostMapping(value="/chosoepeihezhuguan")
	 public Result<String> chosoepeihezhuguan(@RequestBody JieshouDto jieshouDto){
		if(jieshouDto.getPeihezhuguan() !=null){
			Peihebumenbiao peihebumenbiao = peihebumenbiaoService.getById(jieshouDto.getId());
			peihebumenbiao.setPeihezhuguanid(jieshouDto.getPeihezhuguan());
			peihebumenbiaoService.updateById(peihebumenbiao);
			return Result.OK("操作成功！");
		}else{
			return Result.error("请选择责任主管");
		}
	 }
	/**
	 *  编辑
	 *
	 * @param peihebumenbiao
	 * @return
	 */
	@AutoLog(value = "配合部门任务表-编辑")
	@Operation(summary="配合部门任务表-编辑")
	@RequiresPermissions("peihebumenbiao:y_peihebumenbiao:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Peihebumenbiao peihebumenbiao) {
		peihebumenbiaoService.updateById(peihebumenbiao);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "配合部门任务表-通过id删除")
	@Operation(summary="配合部门任务表-通过id删除")
	@RequiresPermissions("peihebumenbiao:y_peihebumenbiao:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		peihebumenbiaoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "配合部门任务表-批量删除")
	@Operation(summary="配合部门任务表-批量删除")
	@RequiresPermissions("peihebumenbiao:y_peihebumenbiao:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.peihebumenbiaoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "配合部门任务表-通过id查询")
	@Operation(summary="配合部门任务表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Peihebumenbiao> queryById(@RequestParam(name="id",required=true) String id) {
		Peihebumenbiao peihebumenbiao = peihebumenbiaoService.getById(id);
		if(peihebumenbiao==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(peihebumenbiao);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param peihebumenbiao
    */
    @RequiresPermissions("peihebumenbiao:y_peihebumenbiao:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Peihebumenbiao peihebumenbiao) {
        return super.exportXls(request, peihebumenbiao, Peihebumenbiao.class, "配合部门任务表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("peihebumenbiao:y_peihebumenbiao:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Peihebumenbiao.class);
    }

}
